<?php
require_once 'common/mvp.inc';
require_once 'pages/TMSAdminProductPage.inc';

class TYandexDirectModule extends TWidget implements IMSAdminPageModule {
/**
 * @var TYandexDirectPublisher
 */
	protected $_ioc_yandex_publisher_;
	
	protected $yandexItem;
	
	public function Caption(){
		return 'Yandex Direct';
	}
	
	public function Banner(){
		if (!$this->yandexItem)
			$this->yandexItem = $this->YandexPublisher->ItemIssue($this->Response()->ModelItem);
		return $this->yandexItem;
	}
	
	public function NeedsToken(){
		return $this->YandexPublisher->NeedsToken();
	}
	
	public function TokenRequestUrl($response_type = 'token',$popup = false){
		return $this->YandexPublisher->TokenRequestUrl('token',$popup);
	}
	
	public function http_PublishToYandex(){
		$this->yandexItem = $this->YandexPublisher->PublishItem($this->Response()->ModelItem, array());
	}
	
	public function http_YandexDirectApply($Campaign,$Title,$Text,$MinusKeywords,$PhraseList = array(),$Stage){
		$this->YandexPublisher->EditItem($this->Banner()->ItemId(), array(
				'Campaign'=>$Campaign,
				'Title'=>$Title,
				'Text'=>$Text,
				'MinusKeywords'=>$MinusKeywords,
				'PhraseList'=>$PhraseList,
				'Stage'=>$Stage
		));
	}
	
	
	/**
	 * default widget output
	*/
	protected function defaultOutput(IThemeEngine $engine){
		if ($this->NeedsToken()){
?>
Для получения доступа к Yandex Direct из магазина пройдите по <a target="_blank" href="<?php echo htmlentities($this->TokenRequestUrl); ?>">ссылке</a>, и после авторизации обновите эту страницу.
<?php }
if ($this->Banner()){
?>
<form method="post" class="editor">
	<input type="hidden" name="controller" value="<?php echo $this->Name();  ?>" />
	<input type="hidden" name="method" value="YandexDirectApply" />
	<div>
		<span class="label">Кампания</span>
			<select class="property-value" name="Campaign" value="<?php echo $this->Banner()->Property('Campaign')->Value(); ?>">
			<?php foreach ($this->Banner()->Property('Campaign')->Selection() as $code => $nm){ ?>
			<option value="<?php echo $code; ?>"<?php if ($code == $this->Banner()->Property('Campaign')->Value()) {?> selected<?php } ?>><?php echo $nm; ?></option>
			<?php } ?>
			</select>
	</div>
	<div><span class="label">Заголовок</span><span></span><input class="property-value length-control" type="text" name="Title" value="<?php echo htmlentities($this->Banner()->Property('Title')->Value()); ?>" /></div>
	<div><span class="label">Описание</span><span></span><textarea class="property-value length-control" type="text" name="Text"><?php echo htmlentities($this->Banner()->Property('Text')->Value()); ?></textarea></div>
	<div><span class="label">Минус-слова</span><textarea class="property-value" name="MinusKeywords"><?php echo htmlentities($this->Banner()->Property('MinusKeywords')->Value()); ?></textarea></div>
	<script language="javascript">
		$(document).ready(function(){
			var lc = $(".length-control");
			lc.each(function(){
				$(this).prev().text("("+$(this).val().length+")");
			});
			lc.keyup(function(){$(this).prev().text("("+$(this).val().length+")")});
		});
	</script>
	<fieldset id="phrases">
		<legend>Фразы</legend>
		<script language="javascript">
			var phrase_id_counter = <?php echo $this->Banner()->NextNewPhraseId; ?>;
			function add_yandex_phrase(){
				$("#phrases").append("<div id=\"ph_<?php echo $this->Banner()->ItemId(); ?>_"+phrase_id_counter+"\"><input type=\"text\" name=\"PhraseList[<?php echo $this->Banner()->ItemId(); ?>_"+phrase_id_counter+"]\" value=\"\" /><a href=\"#\" onclick=\"javascript:del_yandex_phrase(&quot;<?php echo $this->Banner()->ItemId(); ?>_"+phrase_id_counter+"&quot;);return false;\">Удалить</a></div>");
				phrase_id_counter++;
			}
			function del_yandex_phrase(id){
				$("#ph_"+id).remove();
			}
		</script>
		<a href="#" onclick="javascript:add_yandex_phrase();return false;">Добавить</a>
		<?php foreach ($this->Banner()->PhraseList as $phrase_id => $phrase){ ?>
		<div id="ph_<?php echo $phrase_id; ?>">
		<input type="text" name="PhraseList[<?php echo $phrase_id; ?>]" value="<?php echo htmlentities($phrase); ?>" />
		<a href="#" onclick="javascript:del_yandex_phrase(&quot;<?php echo $phrase_id; ?>&quot;);return false;">Удалить</a>
		</div>
		<?php } ?>
	</fieldset>
<?php 
	$p = $this->Banner()->Property('Stage');
?>
	<div>
		<span class="label">Стадия</span>
		<select class="property-value" name="Stage">
		<?php foreach ($p->Selection() as $stagecode => $stagename){ ?>
			<option value="<?php echo $stagecode; ?>" <?php if ($stagecode == $p->Value()) { ?>selected="true"<?php } ?>><?php echo htmlentities($stagename); ?></option>
		<?php } ?>
		</select>
	</div>
	<div><span class="label">Кампания активна: <?php echo $this->Banner()->CampaignStatusShow; ?></span></div>		
	<div><span class="label">Объявление активно: <?php echo $this->Banner()->IsActive; ?></span></div>		
	<div><span class="label">Результат модерации: <?php echo $this->Banner()->StatusBannerModerate; ?></span></div>		
	<div class="btn-holder"><button type="submit">Применить</button></div>
</form>
<?php } else { ?>
<form method="post" />
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="PublishToYandex" />
	<input type="submit" value="Create banner" />
</form>
<?php		
		}
	}	
}